# The planner's problem with infinite horizon and transition path
# With financial frictions

##################DeClaration of Parameters / Variables ###################
# The number of periods the economy converges to the unconstrained steady state
# Since T is large (>=100), the economy converges before reaching T

param T; 
param shock_start;
param shock_duration;

#-- Households' parameters
param BETA;   # discount factor
param SIGMA;  # risk-aversion parameter
param CHI;    # entrepreneur share
param NU;     # labor supply elasticity
param MU;     # disutility of labor supply

#-- Firms' parameters
param A;      # aggregate productivity
param ALPHA;  # capital share
param DELTA;  # depreciation rate, may be set to 1 in the simple scenarios

#-- Financial market parameters
param eta_0;  # constant term in the cost function for matching
param eta_1;  # curvature in the cost function for matching
   
#-- Gov' Parameters
param G;      # government spending in steady state

#-- End point (solved from steady state, but not used)
param K_SS;
param B_SS;
param C_SS;
param L_SS;
param phi_SS;
param eta_SS;
param rho_SS;
param q_SS;
param q_w_SS;

#-- Initial point
param K_ini;
param B_ini;
param tau_k_0;

#-- Fiscal shocks
param fiscal_shock;    # if it is 1, then run fiscal shock; see the .run file
param gov_spending_up; # the magnitude of fiscal shock

#---------------Declaration of Variables (only allocations)----------------

# planner's allocation 
var K {t in -1..T} >= 0;
var B {t in -1..T};
var C {t in 0..T} >= 0;
var L {t in 0..T} >= 0;
var phi {t in 0..T} >= 0, <=1;
var I {t in 0..T}; # investment

# asset market features
var q_w {t in 0..T}  >= 1;
var q {t in 0..T}    >= 1;
var eta {t in 0..T}  >= 0;
var rho {t in 0..T}  >= 0;
var eta_p {t in 0..T};

# policy impementation
var R{t in 0..T};
var tau_k{t in 0..T};
var tau_l{t in 0..T};

# exogenous given variables (note: if we use parameters, we cannot vary them)
var Gov_spending{t in 0..T};

#################### Maximize the objective: Welfare ######################

maximize social_welfare:
(sum{t in 0..T-1} BETA^t * (log(C[t]) - MU * L[t]^(1 + NU) / (1 + NU)))
+ BETA^T / (1 - BETA) * (log(C[T]) - MU * L[T]^(1 + NU) / (1 + NU));


#################### The Planner's Constraints ############################

#----------------------- Definitions
subject to def_eta {t in 0..T}:
eta[t] = eta_0 * phi[t]^eta_1;

subject to def_eta_p {t in 0..T}:
eta_p[t] = eta_0 * eta_1 * phi[t]^(eta_1 - 1);

subject to def_q {t in 0..T}:
q_w[t] = 1 + eta[t] + (1 - phi[t]) * phi[t] * eta_p[t];

subject to def_q_w {t in 0..T}:
q[t] = q_w[t] - eta[t];

subject to def_rho {t in 0..T}:
rho[t] * (1 - phi[t]^2 * eta_p[t]) = eta[t] + phi[t] * eta_p[t];

#------------------------ SOC constraint
subject to soc {t in 0..T}:
C[t] + Gov_spending[t] + (1 + phi[t] * eta[t]) * K[t]
= A * K[t-1]^ALPHA * L[t]^(1 - ALPHA) + (1 + (1 - CHI) * phi[t] * eta[t]) * (1 - DELTA) * K[t-1];

#------------------------- Period 0 constraints
subject to implementability_at_period_0:
C[0]^(-SIGMA) * C[0] - MU * L[0]^NU * L[0] + C[0]^(-SIGMA) * B[0] + C[0]^(-SIGMA) * (1 + phi[0] * eta[0]) * K[0]
= C[0]^(-SIGMA) * B[-1] + C[0]^(-SIGMA) * (1 - tau_k[0]) * A * ALPHA * K[-1]^(ALPHA - 1) * L[0]^(1 - ALPHA) * K[-1]
+ C[0]^(-SIGMA) * (1 + (1 - CHI) * phi[0] * eta[0]) * (1 - DELTA) * K[-1];

subject to fin_constr_at_period_0:
CHI * B[-1] + (1 - (1 - CHI) * phi[0] * q[0]) * (1 - DELTA) * K[-1] - (1 - phi[0] * q[0]) * K[0]
= 0;

#------------------------- Periods 1..T constraints
subject to implementability {t in 1..T}:
C[t]^(-SIGMA) * (C[t] + B[t] + q_w[t] * K[t]) 
= MU * L[t]^NU * L[t] + C[t-1]^(-SIGMA) * (B[t-1] + q_w[t-1] * K[t-1]) / BETA;

subject to fin_constr {t in 1..T}:
CHI * C[t-1]^(-SIGMA) * B[t-1] + BETA * C[t]^(-SIGMA) * (1 + CHI * rho[t]) * (1 - (1 - CHI) * phi[t] * q[t]) * (1 - DELTA) * K[t-1]
- (1 - phi[t] * q[t]) * K[t] * BETA * C[t]^(-SIGMA)  * (1 + CHI * rho[t])
= 0; 

#------------------------- Initial and terminal conditions
subject to initial_capital_tax:
tau_k[0] = tau_k_0;

subject to initial_rate:
R[0] = 1;

subject to ini_point_K:
K[-1] = K_ini;

subject to ini_point_B:
B[-1] = B_ini;


#----------------------- Steady-state conditions
subject to soc_SS:
C[T] + Gov_spending[T] + (1 + phi[T] * eta[T]) * K[T]
= A * K[T]^ALPHA * L[T]^(1 - ALPHA) + (1 + (1 - CHI) * phi[T] * eta[T]) * (1 - DELTA) * K[T];

subject to fin_constr_SS:
CHI * C[T]^(-SIGMA) * B[T] + BETA * C[T]^(-SIGMA) * (1 + CHI * rho[T]) * (1 - (1 - CHI) * phi[T] * q[T]) * (1 - DELTA) * K[T]
- (1 - phi[T] * q[T]) * K[T] * BETA * C[T]^(-SIGMA)  * (1 + CHI * rho[T]) = 0;

subject to implementability_SS:
C[T]^(-SIGMA) * (C[T] + B[T] + q_w[T] * K[T]) 
= MU * L[T]^NU * L[T] + C[T]^(-SIGMA) * (B[T] + q_w[T] * K[T]) / BETA;

#------------------------ Investment, Taxes, and interest rates
subject to investment{t in 0..T}:
I[t] = K[t] - (1 - DELTA) * K[t-1];

subject to interest_rate{t in 0..T-1}:
1 = BETA * C[t+1]^(-SIGMA) / C[t]^(-SIGMA) * R[t+1] * (1 + CHI * rho[t+1]);

subject to capital_taxes{t in 0..T-1}:
q_w[t] * C[t]^(-SIGMA) = BETA * C[t+1]^(-SIGMA) * ((1 - tau_k[t+1]) * ALPHA * A * K[t]^(ALPHA - 1) * L[t+1]^(1 - ALPHA) 
+ (1 - DELTA * (1 - tau_k[t+1])) * q_w[t+1] + CHI * (1 - DELTA) * phi[t+1] * (q[t+1] * (1 + rho[t+1]) - q_w[t+1]));

subject to labor_taxes{t in 0..T}:
(1 - tau_l[t]) * (1 - ALPHA) * K[t-1]^ALPHA * L[t]^(-ALPHA) * C[t]^(-SIGMA) = MU * L[t]^NU;


######################## Shocks #########################

# The constraints when there are no fiscal shocks
subject to no_fiscal_shock{t in 0..T}:
Gov_spending[t] = G;

# The constraints when there are fiscal shocks
subject to fiscal_shock_1 {t in shock_start..shock_start + shock_duration - 1}:
Gov_spending[t] = G * (1 + gov_spending_up);

subject to fiscal_shock_2{t in 0..shock_start - 1 union shock_start + shock_duration..T}:
Gov_spending[t] = G;

